/** CS2340 Spring 2011 Project
 *  Team: 40 Jiggawatt Hard Driver
 *  Suk Hwan Hong
 *  Rhys Saraceni
 *  Travis Cooper
 *  Luke Stubb
 *
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0 
 */

package edu.gatech.jiggawatt.office.gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import edu.gatech.jiggawatt.office.model.Appointment;
import edu.gatech.jiggawatt.office.model.Patient;
import edu.gatech.jiggawatt.office.model.TreatmentRecord;
import edu.gatech.jiggawatt.office.presenter.Presenter;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import javax.swing.JRadioButton;
import java.awt.Font;

/**
 * Represents nurse frame
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0
 *
 */
public class NurseFrame extends JFrame { // $codepro.audit.disable declareDefaultConstructors

	private Presenter presenter;
	private JPanel contentPane;
	private JTextField searchInput;
	private JTextField fNameField;
	private JTextField lNameField;
	private JTextField ageField;
	private JTextField phoneField;
	private JTextField emailField;
	private DefaultListModel appointmentListModel;
	private DefaultListModel trModel;
	private JTextField pharmacyField;
	private JTextField insuranceField;
	private JList patientList;
	private JRadioButton rdbtnMale;
	private JRadioButton rdbtnFemale;
	private ButtonGroup genderGroup;
	private JTextArea addressArea;
	private JButton btnRemovePatient;	
	private JButton btnUpdatePatient;
	private JButton btnViewEditTR;
	private JTextArea allergyArea;
	private JList dailyAppointmentsList;	
	private JList TRList; // $codepro.audit.disable instanceFieldNamingConvention
	private DefaultListModel patientModel;
	private JTextField idField;	
	private int year;
	private int month;
	private int day;
	private JTextField desiredDoctorField;
	private JTextField dateField;
	private JTextField timeField;
	private JTextArea reasonArea;
	private JButton btnCheckInPatient;
	private JButton btnDeleteAppointment;
	private JButton btnEditAppointment;
	private JTextField patientNameField;
		

	/**
	 * Create the frame.
	 * @param p presenter
	 * @param id id of nurse
	 */
	public NurseFrame(Presenter p, String id) {
		presenter = p;
		
		setTitle("Awesome Office"); // $codepro.audit.disable stringLiterals
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600); // $codepro.audit.disable numericLiterals
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); // $codepro.audit.disable numericLiterals
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNurseLoggedIn = new JLabel("Nurse logged in as: " + id); // $codepro.audit.disable stringLiterals
		lblNurseLoggedIn.setBounds(10, 11, 300, 25); // $codepro.audit.disable numericLiterals
		contentPane.add(lblNurseLoggedIn);
		
		JButton btnLogout = new JButton("Logout"); // $codepro.audit.disable stringLiterals
		
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				setVisible(false);
				dispose();
				JFrame loginFrame = new LoginFrame(presenter);
				loginFrame.setVisible(true);
			}
		});
		
		btnLogout.setBounds(190, 13, 120, 23); // $codepro.audit.disable numericLiterals
		contentPane.add(btnLogout);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 47, 784, 515); // $codepro.audit.disable numericLiterals
		contentPane.add(tabbedPane);
		
		JPanel daily_panel = new JPanel();
		tabbedPane.addTab("Daily", null, daily_panel, null); // $codepro.audit.disable stringLiterals
		daily_panel.setLayout(null);
		
		JLabel lblAppointment = new JLabel("Selected Appointment:"); // $codepro.audit.disable stringLiterals
		lblAppointment.setBounds(270, 11, 156, 14); // $codepro.audit.disable numericLiterals
		daily_panel.add(lblAppointment);
		
		JLabel lblDate = new JLabel("Today is: " + getDate()); // $codepro.audit.disable stringLiterals
		lblDate.setBounds(10, 11, 161, 14); // $codepro.audit.disable numericLiterals
		daily_panel.add(lblDate);
		
		JScrollPane scrollPane_dailyAppointments = new JScrollPane();
		scrollPane_dailyAppointments.setBounds(10, 63, 240, 385); // $codepro.audit.disable numericLiterals
		daily_panel.add(scrollPane_dailyAppointments);
		
		appointmentListModel = new DefaultListModel();
		
		Calendar calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH) + 1;		
		day = calendar.get(Calendar.DAY_OF_MONTH);
		
		JPanel appointment_display = new JPanel();
		appointment_display.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		appointment_display.setBounds(260, 36, 509, 436); // $codepro.audit.disable numericLiterals
		daily_panel.add(appointment_display);
		appointment_display.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Desired Doctor:"); // $codepro.audit.disable stringLiterals
		lblNewLabel.setBounds(10, 59, 110, 14); // $codepro.audit.disable numericLiterals
		appointment_display.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Date:"); // $codepro.audit.disable stringLiterals
		lblNewLabel_1.setBounds(10, 115, 46, 14); // $codepro.audit.disable numericLiterals
		appointment_display.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Time:"); // $codepro.audit.disable stringLiterals
		lblNewLabel_2.setBounds(10, 161, 46, 14);
		appointment_display.add(lblNewLabel_2);
		
		JLabel label = new JLabel("Reason For Appointment:"); // $codepro.audit.disable stringLiterals
		label.setBounds(10, 217, 151, 14);
		appointment_display.add(label);
		
		btnCheckInPatient = new JButton("Check In Patient"); // $codepro.audit.disable stringLiterals
		btnCheckInPatient.setEnabled(false);
		btnCheckInPatient.setBounds(10, 402, 137, 23);
		appointment_display.add(btnCheckInPatient);
		
		btnCheckInPatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if (dailyAppointmentsList.getSelectedIndex() == -1)
				{
					JOptionPane.showMessageDialog(null, "You must select an appointment to check in",  // $codepro.audit.disable stringLiterals
							"Error", JOptionPane.ERROR_MESSAGE);					 // $codepro.audit.disable stringLiterals
				}
				else
				{
					JFrame newTreatmentRecordFrame = new newTreatmentRecordFrame(presenter, 
							(Appointment) dailyAppointmentsList.getSelectedValue(), appointmentListModel);
					newTreatmentRecordFrame.setVisible(true);
				}
			}
		});
		
		btnEditAppointment = new JButton("Edit Appointment"); // $codepro.audit.disable stringLiterals
		btnEditAppointment.setEnabled(false);
		btnEditAppointment.setBounds(180, 402, 137, 23);
		appointment_display.add(btnEditAppointment);
		
		btnDeleteAppointment = new JButton("Delete Appointment"); // $codepro.audit.disable stringLiterals
		btnDeleteAppointment.setEnabled(false);
		btnDeleteAppointment.setBounds(352, 402, 137, 23);
		appointment_display.add(btnDeleteAppointment);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 242, 479, 149);
		appointment_display.add(scrollPane);
		
		reasonArea = new JTextArea();
		reasonArea.setEnabled(false);
		reasonArea.setEditable(false);
		scrollPane.setViewportView(reasonArea);
		
		desiredDoctorField = new JTextField();
		desiredDoctorField.setFont(new Font("Tahoma", Font.BOLD, 11)); // $codepro.audit.disable stringLiterals
		desiredDoctorField.setEnabled(false);
		desiredDoctorField.setEditable(false);
		desiredDoctorField.setBounds(10, 74, 479, 20);
		appointment_display.add(desiredDoctorField);
		desiredDoctorField.setColumns(10);
		
		dateField = new JTextField();
		dateField.setFont(new Font("Tahoma", Font.BOLD, 11)); // $codepro.audit.disable stringLiterals
		dateField.setEnabled(false);
		dateField.setEditable(false);
		dateField.setBounds(10, 130, 479, 20);
		appointment_display.add(dateField);
		dateField.setColumns(10);
		
		timeField = new JTextField();
		timeField.setFont(new Font("Tahoma", Font.BOLD, 11)); // $codepro.audit.disable stringLiterals
		timeField.setEnabled(false);
		timeField.setEditable(false);
		timeField.setBounds(10, 186, 479, 20);
		appointment_display.add(timeField);
		timeField.setColumns(10);
		
		JLabel lblPatientName = new JLabel("Patient Name:"); // $codepro.audit.disable stringLiterals
		lblPatientName.setBounds(10, 11, 80, 14);
		appointment_display.add(lblPatientName);
		
		patientNameField = new JTextField();
		patientNameField.setEnabled(false);
		patientNameField.setFont(new Font("Tahoma", Font.BOLD, 11)); // $codepro.audit.disable stringLiterals
		patientNameField.setEditable(false);
		patientNameField.setBounds(10, 28, 479, 20);
		appointment_display.add(patientNameField);
		patientNameField.setColumns(10);
		
		JLabel lblDailyAppointments = new JLabel("Daily Appointments:"); // $codepro.audit.disable stringLiterals
		lblDailyAppointments.setBounds(10, 38, 135, 14);
		daily_panel.add(lblDailyAppointments);
		
		
		for (Appointment app : presenter.getDailySchedule(new edu.gatech.jiggawatt.office.model.Date(year,month,day)).getAppointments())
		{
			if (null != app)
			{
				appointmentListModel.addElement(app);
			}
		}
		
		dailyAppointmentsList = new JList(appointmentListModel);
		scrollPane_dailyAppointments.setViewportView(dailyAppointmentsList);
		
	    dailyAppointmentsList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) 
			{
				if (dailyAppointmentsList.getSelectedIndex() == -1)
				{
					patientNameField.setText("");
					desiredDoctorField.setText("");
					dateField.setText("");
				    timeField.setText("");	
				    reasonArea.setText("");
				    
				    patientNameField.setEnabled(false);
				    desiredDoctorField.setEnabled(false);
				    dateField.setEnabled(false);
				    timeField.setEnabled(false);
				    reasonArea.setEnabled(false);
				    			    
				    btnCheckInPatient.setEnabled(false);
				    btnEditAppointment.setEnabled(false);
				    btnDeleteAppointment.setEnabled(false);
 
				}
				else
				{
					patientNameField.setText(presenter.getUser(((Appointment) dailyAppointmentsList.getSelectedValue()).getId()).getFullName());
					desiredDoctorField.setText((((Appointment) dailyAppointmentsList.getSelectedValue()).getDesiredDoctor()));
					dateField.setText((((Appointment) dailyAppointmentsList.getSelectedValue()).getDate().toString()));
				    timeField.setText((((Appointment) dailyAppointmentsList.getSelectedValue()).getTime().toString()));	
				    reasonArea.setText((((Appointment) dailyAppointmentsList.getSelectedValue()).getReason()));
				    
				    patientNameField.setEnabled(false);
				    desiredDoctorField.setEnabled(false);
				    dateField.setEnabled(false);
				    timeField.setEnabled(false);
				    reasonArea.setEnabled(false);
			    
				    btnCheckInPatient.setEnabled(true);
				    btnEditAppointment.setEnabled(true);
				    btnDeleteAppointment.setEnabled(true);					
				}
			}
		});
		
		btnEditAppointment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if (dailyAppointmentsList.getSelectedIndex() == -1)
				{
					JOptionPane.showMessageDialog(null, "You must select an appointment to edit",  // $codepro.audit.disable stringLiterals
							"Error", JOptionPane.ERROR_MESSAGE);					 // $codepro.audit.disable stringLiterals
				}
				else
				{
					JFrame viewEditAppointmentFrame = new ViewEditAppointmentFrame(presenter, 
							(Appointment) dailyAppointmentsList.getSelectedValue(), appointmentListModel);
					viewEditAppointmentFrame.setVisible(true);
				}		
			}
		});
		
		btnDeleteAppointment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if (dailyAppointmentsList.getSelectedIndex() == -1)
				{
					JOptionPane.showMessageDialog(null, "You must select an appointment to remove",  // $codepro.audit.disable stringLiterals
							"Error", JOptionPane.ERROR_MESSAGE);					 // $codepro.audit.disable stringLiterals
				}
				else
				{
					Appointment selectedApp = (Appointment) dailyAppointmentsList.getSelectedValue();
					presenter.removeAppointment(selectedApp.getDate(), selectedApp.getTime());
					
					appointmentListModel.clear();

					for (Appointment app : presenter.getDailySchedule(new edu.gatech.jiggawatt.office.model.Date(year,month,day)).getAppointments())
					{
						if (null != app)
						{
							appointmentListModel.addElement(app);
						}
					}
				}				
			}
		});
	    
		JPanel patients_panel = new JPanel();
		tabbedPane.addTab("Patients", null, patients_panel, null); // $codepro.audit.disable stringLiterals
		patients_panel.setLayout(null);
		
		JLabel lblSearch = new JLabel("Search by name (last first):"); // $codepro.audit.disable stringLiterals
		lblSearch.setBounds(10, 11, 206, 14);
		patients_panel.add(lblSearch);
		
		JLabel lblPatientDetails = new JLabel("Patient Details:"); // $codepro.audit.disable stringLiterals
		lblPatientDetails.setBounds(261, 11, 97, 14);
		patients_panel.add(lblPatientDetails);
		
		searchInput = new JTextField();
		searchInput.setBounds(10, 32, 107, 20);
		patients_panel.add(searchInput);
		searchInput.setColumns(10);
		
		JButton btnAddPatient = new JButton("Add Patient"); // $codepro.audit.disable stringLiterals
		btnAddPatient.setBounds(10, 453, 117, 23);
		patients_panel.add(btnAddPatient);
		
		btnRemovePatient = new JButton("Remove Patient"); // $codepro.audit.disable stringLiterals
		btnRemovePatient.setEnabled(false);
		btnRemovePatient.setBounds(137, 453, 114, 23);
		patients_panel.add(btnRemovePatient);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 63, 240, 379);
		patients_panel.add(scrollPane_1);
		
		patientModel = new DefaultListModel();
		for (Patient e : presenter.getPatientList())
		{
			patientModel.addElement(e);
		}
		
		patientList = new JList(patientModel);
		scrollPane_1.setViewportView(patientList);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel.setBounds(260, 36, 509, 440);
		patients_panel.add(panel);
		panel.setLayout(null);
		
		JLabel label_1 = new JLabel("First Name:"); // $codepro.audit.disable stringLiterals
		label_1.setBounds(20, 40, 86, 14);
		panel.add(label_1);
		
		JLabel label_2 = new JLabel("Last Name:"); // $codepro.audit.disable stringLiterals
		label_2.setBounds(20, 74, 86, 14);
		panel.add(label_2);
		
		JLabel label_3 = new JLabel("Age:"); // $codepro.audit.disable stringLiterals
		label_3.setBounds(20, 106, 46, 14);
		panel.add(label_3);
		
		JLabel label_4 = new JLabel("Gender:"); // $codepro.audit.disable stringLiterals
		label_4.setBounds(20, 135, 46, 14);
		panel.add(label_4);
		
		JLabel label_5 = new JLabel("Address:"); // $codepro.audit.disable stringLiterals
		label_5.setBounds(20, 160, 46, 14);
		panel.add(label_5);
		
		JLabel label_6 = new JLabel("Phone:"); // $codepro.audit.disable stringLiterals
		label_6.setBounds(20, 229, 46, 14);
		panel.add(label_6);
		
		JLabel label_7 = new JLabel("E-mail:"); // $codepro.audit.disable stringLiterals
		label_7.setBounds(20, 260, 46, 14);
		panel.add(label_7);
		
		JLabel lblTreatmentRecords = new JLabel("Treatment Records:");
		lblTreatmentRecords.setBounds(282, 14, 195, 14);
		panel.add(lblTreatmentRecords);
		
		fNameField = new JTextField();
		fNameField.setEditable(false);
		fNameField.setColumns(10);
		fNameField.setBounds(99, 41, 154, 20);
		panel.add(fNameField);
		
		lNameField = new JTextField();
		lNameField.setEditable(false);
		lNameField.setColumns(10);
		lNameField.setBounds(99, 72, 154, 20);
		panel.add(lNameField);
		
		ageField = new JTextField();
		ageField.setEditable(false);
		ageField.setColumns(10);
		ageField.setBounds(99, 103, 154, 20);
		panel.add(ageField);
		
		phoneField = new JTextField();
		phoneField.setEditable(false);
		phoneField.setColumns(10);
		phoneField.setBounds(99, 226, 154, 20);
		panel.add(phoneField);
		
		emailField = new JTextField();
		emailField.setEditable(false);
		emailField.setColumns(10);
		emailField.setBounds(99, 257, 154, 20);
		panel.add(emailField);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(99, 160, 154, 55);
		panel.add(scrollPane_2);
		
		addressArea = new JTextArea();
		addressArea.setEditable(false);
		scrollPane_2.setViewportView(addressArea);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(282, 48, 203, 347);
		panel.add(scrollPane_3);
		
		trModel = new DefaultListModel();
		
		TRList = new JList(trModel);
		scrollPane_3.setViewportView(TRList);
		
		btnViewEditTR = new JButton("View/Edit Treatment Record");
		btnViewEditTR.setEnabled(false);
		btnViewEditTR.setBounds(282, 406, 203, 23);
		panel.add(btnViewEditTR);
		
		JLabel lblPharmacy = new JLabel("Pharmacy:");
		lblPharmacy.setBounds(20, 291, 72, 14);
		panel.add(lblPharmacy);
		
		JLabel lblInsurance = new JLabel("Insurance:");
		lblInsurance.setBounds(20, 325, 72, 14);
		panel.add(lblInsurance);
		
		JLabel lblAllergy = new JLabel("Allergy:");
		lblAllergy.setBounds(20, 353, 46, 14);
		panel.add(lblAllergy);
		
		pharmacyField = new JTextField();
		pharmacyField.setEditable(false);
		pharmacyField.setBounds(99, 288, 154, 20);
		panel.add(pharmacyField);
		pharmacyField.setColumns(10);
		
		insuranceField = new JTextField();
		insuranceField.setEditable(false);
		insuranceField.setBounds(99, 322, 154, 20);
		panel.add(insuranceField);
		insuranceField.setColumns(10);
		
		JScrollPane scrollPane_4 = new JScrollPane();
		scrollPane_4.setBounds(99, 353, 154, 42);
		panel.add(scrollPane_4);
		
		allergyArea = new JTextArea();
		allergyArea.setEditable(false);
		scrollPane_4.setViewportView(allergyArea);
		
		patientList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) 
			{
				if (patientList.getSelectedIndex() == -1)
				{
					idField.setText("");
					fNameField.setText("");
				    lNameField.setText("");	
				    ageField.setText("");
				    rdbtnMale.setSelected(false);
				    rdbtnFemale.setSelected(false);
				    genderGroup.clearSelection();
				    addressArea.setText("");
				    phoneField.setText("");
				    emailField.setText("");
				    pharmacyField.setText("");
				    insuranceField.setText("");
				    allergyArea.setText("");
					fNameField.setEditable(false);
				    lNameField.setEditable(false);
				    ageField.setEditable(false);				    				    
				    addressArea.setEditable(false);
				    phoneField.setEditable(false);
				    emailField.setEditable(false);
				    pharmacyField.setEditable(false);
				    insuranceField.setEditable(false);
				    allergyArea.setEditable(false);

				    rdbtnMale.setEnabled(false);
				    rdbtnFemale.setEnabled(false);
			    
				    btnRemovePatient.setEnabled(false);
				    btnUpdatePatient.setEnabled(false);
				    
				    trModel.clear(); 
				}
				else
				{
					idField.setText((((Patient) patientList.getSelectedValue()).getId()));
					fNameField.setText((((Patient) patientList.getSelectedValue()).getFirstName()));
				    lNameField.setText((((Patient) patientList.getSelectedValue()).getLastName()));
				    ageField.setText(Integer.toString(((Patient) patientList.getSelectedValue()).getAge()));
				    if (((Patient) patientList.getSelectedValue()).getGender() == 0)
				    {
				    	rdbtnMale.setSelected(true);
				    }
				    else
				    {
				    	rdbtnFemale.setSelected(true);
				    }
				    
				    addressArea.setText((((Patient) patientList.getSelectedValue()).getAddress()));
				    phoneField.setText((((Patient) patientList.getSelectedValue()).getPhone()));
				    emailField.setText((((Patient) patientList.getSelectedValue()).getEmail()));
				    pharmacyField.setText((((Patient) patientList.getSelectedValue()).getPharmacy()));
				    insuranceField.setText((((Patient) patientList.getSelectedValue()).getInsurance()));
				    allergyArea.setText((((Patient) patientList.getSelectedValue()).getAllergies()));
				    
					fNameField.setEditable(true);
				    lNameField.setEditable(true);
				    ageField.setEditable(true);
				    addressArea.setEditable(true);
				    phoneField.setEditable(true);
				    emailField.setEditable(true);
				    pharmacyField.setEditable(true);
				    insuranceField.setEditable(true);
				    allergyArea.setEditable(true);

				    rdbtnMale.setEnabled(true);
				    rdbtnFemale.setEnabled(true);

				    btnRemovePatient.setEnabled(true);
				    btnUpdatePatient.setEnabled(true);
				    
				    trModel.clear(); 
				    for (TreatmentRecord tr : presenter.getTreatmentRecords(((Patient) patientList.getSelectedValue()).getId()))
				    {
				    	trModel.addElement(tr);
				    }				    		
				}
			}
		});
		
		TRList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) 
			{
				if (TRList.getSelectedIndex() == -1)
				{
					btnViewEditTR.setEnabled(false);
				}
				else
				{
					btnViewEditTR.setEnabled(true);
				}
			}
		});
		
		btnUpdatePatient = new JButton("Update Patient");
		btnUpdatePatient.setEnabled(false);
		btnUpdatePatient.setBounds(99, 406, 154, 23);
		panel.add(btnUpdatePatient);
		
		btnUpdatePatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				try
				{
					int gender;
					if (rdbtnMale.isSelected())
					{
						gender = 0;
					}
					else
					{
						gender = 1;
					}
					Patient updatedPatient = new Patient(idField.getText(), null, fNameField.getText(), lNameField.getText(), 
							Integer.parseInt(ageField.getText()), gender, addressArea.getText(),
							phoneField.getText(), emailField.getText());
					
				    updatedPatient.setPharmacy(pharmacyField.getText());
				    updatedPatient.setInsurance(insuranceField.getText());
				    updatedPatient.setAllergies(allergyArea.getText());

				    if (!presenter.updateUser(updatedPatient))
					{
						JOptionPane.showMessageDialog(null, "User to update doesn't exist", 
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					else
					{
						patientModel.clear();

						for (Patient e : presenter.getPatientList())
						{
							patientModel.addElement(e);
						}
					}
				}
				catch (NumberFormatException e)
				{
					JOptionPane.showMessageDialog(null, "You must enter integers for age", 
							"Error", JOptionPane.ERROR_MESSAGE);			
				}
			}
		});
		
		btnAddPatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				JFrame newPatientFrame = new newPatientFrame(presenter, patientModel);
				newPatientFrame.setVisible(true);
			}
		});
		
		btnRemovePatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if (patientList.getSelectedIndex() == -1)
				{
					JOptionPane.showMessageDialog(null, "You must select the patient to remove", 
						"Error", JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					String id = ((Patient) patientList.getSelectedValue()).getId();
					presenter.removeUser(id);
					
					patientModel.clear();

					for (Patient e : presenter.getPatientList())
					{
						patientModel.addElement(e);
					}
				}
			}
		});
		
		btnViewEditTR.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				String id = ((Patient) patientList.getSelectedValue()).getId();
				JFrame editTreatmentRecordFrame = new editTreatmentRecordFrame(presenter, id, (TreatmentRecord) TRList.getSelectedValue(), trModel);
				editTreatmentRecordFrame.setVisible(true);
			}
		});
		
		rdbtnMale = new JRadioButton("Male");
		rdbtnMale.setBounds(99, 130, 64, 23);
		panel.add(rdbtnMale);
		
		rdbtnFemale = new JRadioButton("Female");
		rdbtnFemale.setBounds(165, 130, 88, 23);
		panel.add(rdbtnFemale);
		
		genderGroup = new ButtonGroup();
		genderGroup.add(rdbtnMale);
		genderGroup.add(rdbtnFemale);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(20, 14, 46, 14);
		panel.add(lblId);
		
		idField = new JTextField();
		idField.setEditable(false);
		idField.setBounds(99, 11, 154, 20);
		panel.add(idField);
		idField.setColumns(10);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.setBounds(127, 31, 89, 23);
		patients_panel.add(btnSearch);
		
		btnSearch.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				if (searchInput.getText().equals(""))
				{
					JOptionPane.showMessageDialog(null, "You must enter name to search for", 
							"Error", JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					Patient foundUser = presenter.getPatient(searchInput.getText());
					if (foundUser == null)
					{
						JOptionPane.showMessageDialog(null, "There is no such patient", 
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					else
					{
						patientList.setSelectedValue(foundUser, true);
						searchInput.setText("");
					}
				}
					
			}
		});
	}
	
	/**
	 * returns string format of today's date
	 * @return string format of today's date
	 */
	private String getDate()
	{
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		Date date = new Date();

		return dateFormat.format(date);
	}
}
